package sisi.invoices;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Window;

import sisi.yacht.Anayacht;
import sisi.yacht.AnayachtController;

/**
 * CustomersCardWindow.java - Controller class for <i>customer's card
 * window</i>.
 * 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 14/5/2015
 * 
 */

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class CustomersCardWindow extends Window implements
		org.zkoss.zk.ui.ext.AfterCompose {
	// window
	// private Window windowCustomersCard;
	private Listbox boxCustomer;

	protected Map<String, Object> args;

	public void onCreate$windowCustomersCard(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();

		// carico lista yachts
		//List<Anayacht> yachts = new AnayachtController().getAnayachtList(null);
		List yachts2 = getListaYachtsWithInvoices();
		ListModelList lm = new ListModelList();
		lm.addAll(yachts2);
		boxCustomer.setModel(lm);
		boxCustomer.setItemRenderer(new RendererYachts());
		boxCustomer.setSelectedIndex(-1);

	}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);
	}

	public List getListaYachtsWithInvoices() {

		EntityManager em = new sisi.MyEmf().getEntityManager();
		try {
			String cQuery = "";

			cQuery += "select anayacht.id, anayacht.name, anayacht.corporation " +
					"FROM anayacht INNER JOIN custinvoice on anayacht.id =  custinvoice.custid " +
					"GROUP BY anayacht.id, anayacht.name, anayacht.corporation, custinvoice.custid, anayacht.del " +
					"HAVING anayacht.del = false ORDER BY anayacht.name";

			javax.persistence.Query q = em.createNativeQuery(cQuery);

			List val = q.getResultList();
			return val;
		} finally {
			em.close();
		}
	}

	
	public void onSelect$boxCustomer(){
		System.out.println("qui..."); 
		System.out.println( boxCustomer.getSelectedIndex() ) ;
		Listitem yachtSelected = boxCustomer.getItemAtIndex(boxCustomer.getSelectedIndex());
		int i = 1;
		
	}
	
	// RENDERERS
	public class RendererYachts implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {

			//Anayacht yacht = ((Anayacht) data);
			Object[] oYacht = (Object[])data;
			String nameYacht = (String)oYacht[1];
			Integer idYacht = (Integer)oYacht[0];
			String corporation = (String)oYacht[2];

			new Listcell(nameYacht + " - Id: |" + idYacht + "| - Corp: "+corporation )
					.setParent(li);
			li.setAttribute("rigaAnayacht", data); 
		}
	}

}
